package com.begye.smartloan.apptest.shenhezhong.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.begye.smartloan.apptest.MyApplication
import com.begye.smartloan.apptest.R
import com.begye.smartloan.apptest.ext.isUrl
import com.begye.smartloan.apptest.ext.toNumberEncrypt
import org.json.JSONObject

class ShenHeZhongQianBaoAdapter(var dataList: List<JSONObject>) :
    RecyclerView.Adapter<ShenHeZhongQianBaoAdapter.ViewHolder>() {

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.list_item_shen_he_zhong_qian_bao, parent, false)
        return ViewHolder(view)
    }

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        private val listItemShenHeZhongQianBaoName: TextView =
            view.findViewById(R.id.listItemShenHeZhongQianBaoName)

        private val listItemShenHeZhongQianBaoIcon: ImageView =
            view.findViewById(R.id.listItemShenHeZhongQianBaoIcon)

        private val listItemShenHeZhongQianBaoNumber: TextView =
            view.findViewById(R.id.listItemShenHeZhongQianBaoNumber)


        @SuppressLint("SetTextI18n")
        fun bindData(position: Int, item: JSONObject) {
            listItemShenHeZhongQianBaoName.text = item.optString("name", "")

            val number = item.optString("account", "").toNumberEncrypt()
            listItemShenHeZhongQianBaoNumber.text = "+${MyApplication.pCode} $number"

            val logo = item.optString("logo", "")
            if (logo.isUrl()) {
                Glide.with(listItemShenHeZhongQianBaoIcon.context)
                    .load(logo)
                    .into(listItemShenHeZhongQianBaoIcon)
            }
        }
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bindData(position, dataList[position])
    }

    override fun getItemCount(): Int {
        return dataList.size
    }
}